/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package my.javax.microedition.contactless.ndef;

/**
 *
 * @author emarkki
 */
public class NDEFRecordType {

    public static final int EMPTY = 0;
    public static final int EXTERNAL_RTD = 4;
    public static final int MIME = 1;
    public static final int NFC_FORUM_RTD = 3;
    public static final int UNKNOWN = 5;
    public static final int URI = 2;

    private int type;
    private String name;

    private static final String[] prefixes = new String[] {
        "",
        "",
        "",
        "urn:nfc:wkt:",
        "urn:nfc:ext:",
        ""
    };

    public NDEFRecordType(int type, String name) {
        this.type = type;
        if(type >= prefixes.length || type < 0) {
            throw new IllegalArgumentException("Unknown type: " + type);
        }
        if(!name.startsWith(prefixes[type])) {
            throw new IllegalArgumentException("Type " + type + " should start with " + prefixes[type]);
        }
        this.name = name.substring(prefixes[type].length());
    }

    public String getName() {
        return name;
    }

    /** Not standard, TODO: hide this method */
    public int getType() {
        return type;
    }

    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final NDEFRecordType other = (NDEFRecordType) obj;
        if (this.type != other.type) {
            return false;
        }
        if (this.name != other.name && (this.name == null || !this.name.equals(other.name))) {
            return false;
        }
        return true;
    }

    public int hashCode() {
        int hash = 5;
        hash = 11 * hash + this.type;
        hash = 11 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    public String toString() {
        return "RecordType["+getType()+","+getName()+"]";
    }
}
